
// 用户ID
let id = getUrlParam("id");
$(function () {
    GetUserRole();
    // loadFnc();
});
// 获取路由信息
let routedata = window.location.href.substring(window.location.href.lastIndexOf('/'))
//获取用户权限
function GetUserRole() {
    // let thisdata = [];
    // thisdata.push(InitRole(id));
    let USERdata = InitRole(id);
    if (USERdata != null) {
        loadFnc();
    }
    else OpenErrmsg("web/errhtml/404.html");
}
function loadFnc() {
    InitCombox();
    LoadStatus();
}
var SearchTypeA = ''
var SearchTypeB = ''
var SearchTypeXD = ''
var SearchTypeLQ = ''
setInterval(() => {
    InitChartYSJ_A(SearchTypeA);
    InitChartYSJ_B(SearchTypeB)
    InitChart_XD(SearchTypeXD)
    InitChart_LQ(SearchTypeLQ)
    LoadStatus();
}, 5000);
function InitCombox() {
    $('#cmb_SearchTypeA').combobox({
        onSelect: function (rec) {
            InitChartYSJ_A(rec.value);
            SearchTypeA = rec.value
        },
    });
    $('#cmb_SearchTypeB').combobox({
        onSelect: function (rec) {
            InitChartYSJ_B(rec.value);
            SearchTypeB = rec.value
        },
    });
    $('#cmb_SearchType_XD').combobox({
        onSelect: function (rec) {
            InitChart_XD(rec.value);
            SearchTypeXD = rec.value
        },
    });
    $('#cmb_SearchType_LQ').combobox({
        onSelect: function (rec) {
            InitChart_LQ(rec.value);
            SearchTypeLQ = rec.value
        },
    });
}
//加载状态
function LoadStatus() {
    let data = {
        "result": 0,
        "data": {
            "Status": "开启"
        },
        "error_msg": ""
    }
    $('#sp_status').html(data.data.Status);
}
// 请求折线图数据
let path = getDataPath();
function getChartData(Type, position) {
    let xAxis = [];
    let data1 = [];
    let data2 = [];
    let data3 = [];
    let data4 = [];
    let url = ''
    url = path + '/ls172/data-analysis/DataAnLineChartA'
    $.ajax({
        url: url,
        async: false,
        type: "GET",
        success: function (data) {
            // console.log(data.data.data[5].vals);
            xAxis = data.data.time
            if (position == 'A') {
                if (Type == 'temp_E') {
                    data1 = data.data.data[0].vals
                    // 趋势分析
                    if (data.data.data[17].vals[3] == 0) {
                        document.getElementById('qushicolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorA').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[5].vals[3] == 0) {
                        document.getElementById('tiaobiancolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorA').style.background = 'red'
                    }
                } else if (Type == 'temp_O') {
                    data1 = data.data.data[1].vals
                    // 趋势分析
                    if (data.data.data[20].vals[3] == 0) {
                        document.getElementById('qushicolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorA').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[4].vals[3] == 0) {
                        document.getElementById('tiaobiancolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorA').style.background = 'red'
                    }
                } else {
                    data1 = data.data.data[2].vals
                    // 趋势分析
                    if (data.data.data[18].vals[3] == 0) {
                        document.getElementById('qushicolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorA').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[3].vals[3] == 0) {
                        document.getElementById('tiaobiancolorA').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorA').style.background = 'red'
                    }
                }
                createChart("chartdiv1", xAxis, "数据拟合曲线图", data1);
                // 压缩机B
            } else if (position == 'B') {
                if (Type == 'temp_E') {
                    data2 = data.data.data[6].vals
                    // 趋势分析
                    if (data.data.data[25].vals[3] == 0) {
                        document.getElementById('qushicolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorB').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[5].vals[3] == 0) {
                        document.getElementById('tiaobiancolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorB').style.background = 'red'
                    }
                } else if (Type == 'temp_O') {
                    data2 = data.data.data[7].vals
                    // 趋势分析
                    if (data.data.data[21].vals[3] == 0) {
                        document.getElementById('qushicolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorB').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[10].vals[3] == 0) {
                        document.getElementById('tiaobiancolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorB').style.background = 'red'
                    }
                } else {
                    data2 = data.data.data[8].vals
                    // 趋势分析
                    if (data.data.data[19].vals[3] == 0) {
                        document.getElementById('qushicolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorB').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[9].vals[3] == 0) {
                        document.getElementById('tiaobiancolorB').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorB').style.background = 'red'
                    }
                }
                createChart("chartdiv2", xAxis, "数据拟合曲线图", data2);
            } else if (position == 'Scrubber') {
                if (Type == 'Liquid_L') {
                    data3 = data.data.data[11].vals
                    // 趋势分析
                    if (data.data.data[23].vals[3] == 0) {
                        document.getElementById('qushicolorLiquid').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorLiquid').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[12].vals[3] == 0) {
                        document.getElementById('tiaobiancolorLiquid').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorLiquid').style.background = 'red'
                    }
                } else if (Type == 'press_O') {
                    data3 = data.data.data[13].vals
                    // 趋势分析
                    if (data.data.data[22].vals[3] == 0) {
                        document.getElementById('qushicolorLiquid').style.background = '#0f0'
                    } else {
                        document.getElementById('qushicolorLiquid').style.background = 'red'
                    }
                    // 跳变分析
                    if (data.data.data[14].vals[3] == 0) {
                        document.getElementById('tiaobiancolorLiquid').style.background = '#0f0'
                    } else {
                        document.getElementById('tiaobiancolorLiquid').style.background = 'red'
                    }
                }
                createChart("chartdiv3", xAxis, "数据拟合曲线图", data3);
            } else {
                data4 = data.data.data[15].vals
                // 趋势分析
                if (data.data.data[24].vals[3] == 0) {
                    document.getElementById('qushicolorAfterColder').style.background = '#0f0'
                } else {
                    document.getElementById('qushicolorAfterColder').style.background = 'red'
                }
                // 跳变分析
                if (data.data.data[16].vals[3] == 0) {
                    document.getElementById('tiaobiancolorAfterColder').style.background = '#0f0'
                } else {
                    document.getElementById('tiaobiancolorAfterColder').style.background = 'red'
                }
                createChart("chartdiv4", xAxis, "数据拟合曲线图", data4);
            }

        },
        error: function (data) {
            console.log("code", data);
        }
    })
}
//压缩机A
function InitChartYSJ_A(Type) {
    if (Type == "temp_E") {
        getChartData(Type, 'A')
    } else if (Type == "temp_O") {
        getChartData(Type, 'A')
    } else if (Type == "press_O") {
        getChartData(Type, 'A')
    }

}
//压缩机B
function InitChartYSJ_B(Type) {
    if (Type == "temp_E") {
        getChartData(Type, 'B')
    } else if (Type == "temp_O") {
        getChartData(Type, 'B')
    } else if (Type == "press_O") {
        getChartData(Type, 'B')
    }

}
//洗涤器
function InitChart_XD(Type) {
    if (Type == "Liquid_L") {
        getChartData(Type, 'Scrubber')
    } else if (Type == "press_O") {
        getChartData(Type, 'Scrubber')
    }

}
//后冷却器
function InitChart_LQ(Type) {
    if (Type == "temp_O") {
        getChartData(Type, 'Aftercooler')
    }
}
function createChart(id, xAxis, title, data) {
    let myChart = echarts.init(document.getElementById(id));
    let newxAxis = []
    for (let i = 0; i < xAxis.length; i++) {
        newxAxis.push(xAxis[i].substr(10, 18))
    }
    let option = {
        title: {

        },
        tooltip: {
            trigger: 'axis'
        },
        // legend: {
        //     data: '温度',
        // },
        grid: {
            left: '1%',
            right: '5%',
            bottom: '1%',
            top: '22%',
            containLabel: true
        },
        toolbox: {
            // feature: {
            //     saveAsImage: {}
            // }
        },
        title: {
            text: title,
            padding: 8,
            textStyle: {
                fontSize: 12,
                height: 15,
                color: '#333'          // 主标题文字颜色
            },
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: newxAxis
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: data,
                type: 'line'
            }
        ]
    }
    myChart.setOption(option);
}